package apig.client.prof;

import java.util.*;

import org.gwm.client.impl.*;

import apig.client.*;
import apig.client.serviface.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.RichTextArea.ExtendedFormatter;

public class CodeInternalFrame extends DefaultGInternalFrame {

	private Code c;
	private TabPanel content;

	public CodeInternalFrame(Code c) {
		super(c.name);
		this.c = c;
		setSize(800, 500);
		content = new TabPanel();
		VerticalPanel contentWrapper = new VerticalPanel();
		contentWrapper.setWidth("100%");
		contentWrapper.setHeight("100%");
		contentWrapper.add(content);
		contentWrapper.setBorderWidth(2);
		content.setWidth("100%");
		content.setHeight("100%");
		contentWrapper.setCellWidth(content, "100%");
		contentWrapper.setCellHeight(content, "100%");
		setContent(contentWrapper);
		Stubman.getStubman()
				.getService()
				.getHypos(c.codeId,
						new AsyncCallback<HashMap<Integer, List<Hypo>>>() {
							@Override
							public void onSuccess(
									HashMap<Integer, List<Hypo>> result) {
								onHyposGotten(result);
							}

							@Override
							public void onFailure(Throwable caught) {
								System.out.println("fail: "
										+ caught.getMessage());
							}
						});
	}

	private void onHyposGotten(HashMap<Integer, List<Hypo>> result) {
		int count = 1;
		for (Integer i : result.keySet()) {
			List<Hypo> hypos = result.get(i);
			content.add(new CodeComparator(hypos), Integer.toString(count));
			count++;
		}
	}

	private class CodeComparator extends VerticalPanel {

		private int hypoIndex = 0;
		private HTML leftHtml = new HTML();
		private HTML rightHtml = new HTML();
		private Button leftButton = new Button();
		private Button rightButton = new Button();
		private List<Hypo> h;
		private Code code1;
		private Code code2;
		private Label fromWhoLabel = new Label();
		private Label fromWhereLabel = new Label();
		private Label hypoIndexator = new Label();

		private String[] leftCodeColoured;
		private String[] rightCodeColoured;

		public CodeComparator(List<Hypo> h) {
			this.h = h;
			code1 = c;
			leftCodeColoured = new String[h.size()];
			rightCodeColoured = new String[h.size()];
			Stubman.getStubman().getService()
					.getCode(h.get(0).code2, new AsyncCallback<Code>() {
						@Override
						public void onSuccess(Code result) {
							if (result == null) {
								System.out.println("null result");
							}
							code2 = result;
							createColouredCode();
							createUI();
						}

						@Override
						public void onFailure(Throwable caught) {
							System.out.println("fail: " + caught.getMessage());
						}
					});
			Stubman.getStubman().getService()
					.getCodeOwner(c.codeId, new AsyncCallback<String>() {

						@Override
						public void onSuccess(String result) {
							fromWhoLabel.setText(result);
						}

						@Override
						public void onFailure(Throwable caught) {
							System.out.println("fail: " + caught.getMessage());
						}
					});
		}

		private void createColouredCode() {
			String leftCode = code1.text;
			String rightCode = code2.text;
			int i = 0;
			for (Hypo hypo : h) {
				if (hypo.code1start != Hypo.UNKNOWN
						&& hypo.code1end != Hypo.UNKNOWN
						&& hypo.code2start != Hypo.UNKNOWN
						&& hypo.code2end != Hypo.UNKNOWN) {
					leftCodeColoured[i] = leftCode
							.substring(0, hypo.code1start)
							+ "<font style=\"background-color: green\">"
							+ leftCode.substring(hypo.code1start,
									hypo.code1end + 1)
							+ "</font>"
							+ leftCode.substring(hypo.code1end + 1,
									leftCode.length());
					rightCodeColoured[i] = rightCode.substring(0,
							hypo.code2start)
							+ "<font style=\"background-color: green\">"
							+ rightCode.substring(hypo.code2start,
									hypo.code2end + 1)
							+ "</font>"
							+ rightCode.substring(hypo.code2end + 1,
									leftCode.length());
				} else {
					leftCodeColoured[i] = leftCode;
					rightCodeColoured[i] = rightCode;
				}
				i++;
			}
		}

		private void createUI() {

			HorizontalPanel dataPanel = new HorizontalPanel();
			dataPanel.add(fromWhoLabel);
			dataPanel.add(fromWhereLabel);
			dataPanel.add(hypoIndexator);
			fromWhereLabel.setText("Плагиат из: " + code2.name + "; ревизия: "
					+ code2.revision);
			HorizontalPanel stub = new HorizontalPanel();
			stub.setWidth("100%");
			dataPanel.setCellWidth(stub, "100%");
			dataPanel.add(stub);
			add(dataPanel);

			HorizontalPanel split = new HorizontalPanel();
			ScrollPanel leftScroll = new ScrollPanel(leftHtml);
			leftScroll.setHeight("450px");
			split.add(leftScroll);
			split.setCellHeight(leftScroll, "100%");
			split.setCellWidth(leftScroll, "50%");
			ScrollPanel rightScroll = new ScrollPanel(rightHtml);
			rightScroll.setHeight("450px");
			split.add(rightScroll);
			split.setCellHeight(rightScroll, "100%");
			split.setCellWidth(rightScroll, "50%");
			setCellWidth(split, "100%");
			setCellHeight(split, "100%");
			split.setWidth("100%");
			split.setHeight("100%");
			add(split);

			HorizontalPanel buttonPanel = new HorizontalPanel();
			buttonPanel.add(leftButton);
			buttonPanel.add(rightButton);
			leftButton.setText("Влево");
			rightButton.setText("Вправо");
			add(buttonPanel);
			leftButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					hypoIndex--;
					handleIndexChange();
				}
			});
			rightButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					hypoIndex++;
					handleIndexChange();
				}
			});

			handleIndexChange();
		}

		private void handleIndexChange() {
			if (leftCodeColoured.length == 1) {
				leftButton.setEnabled(false);
				rightButton.setEnabled(false);
			} else if (hypoIndex <= 0) {
				hypoIndex = 0;
				leftButton.setEnabled(false);
				rightButton.setEnabled(true);
			} else if (hypoIndex >= leftCodeColoured.length - 1) {
				hypoIndex = leftCodeColoured.length - 1;
				leftButton.setEnabled(true);
				rightButton.setEnabled(false);
			} else {
				leftButton.setEnabled(true);
				rightButton.setEnabled(true);
			}
			leftHtml.setHTML(leftCodeColoured[hypoIndex]);
			rightHtml.setHTML(rightCodeColoured[hypoIndex]);
			hypoIndexator.setText("[" + (hypoIndex + 1) + " гипотеза из "
					+ leftCodeColoured.length);
		}
	}
}
